import csv,os,re,glob,datetime

files=[]
readers=[]
data={'airport':[{'type':[{'date':[]}]}]}
timere=re.compile('-\d{4}-')
aport=re.compile('EDI|ABZ|GLA')
typeof=re.compile('Arr|Dep')

def getDate(filename):
   return timere.search(filename).group()[1:5]

def getAirport(filename):
   return str(aport.search(filename).group())

def getType(filename):
   return str(typeof.search(filename).group())



def getData(readid):
   temp={}
   for row in readid:
      if row[3]!='XXXX':
         row[2]=row[2][0:2]+row[2][3:5]
         temp1=datetime.datetime.strptime(row[3],'%H%M')
         temp2=datetime.datetime.strptime(row[2],'%H%M')
         tempdiff=temp1-temp2
         tempmins=tempdiff.seconds/60
         if tempmins>(60*12): #if over 12 hours, must be early
            tempmins=1440-tempmins
            diff='early'
         else:
            diff='late'
               
         row.append(tempmins)
         row.append(diff)
         temp[row[0]]=row
         
   return temp
   
for fileid in glob.glob('/home/scm-studs/f6100782/python/*.csv'):
   files.append(open(fileid,'r'))

for csvs in files:
   readers.append(csv.reader(csvs,delimiter=','))

for csvsid,readerid,num in zip(files,readers,range(0,len(files))):
   temp1=getData(readerid)
   temp2={getDate(str(csvsid)):temp1}
   temp3={getType(str(csvsid)):temp2}
   
   data[getAirport(str(csvsid))+getType(str(csvsid))]=temp2
                                 
   #      data[getAirport(str(csvsid))][getType(str(csvsid))][getDate(str(csvsid))]=getData(readerid)
      

for flights in data['EDIDep']['2809'].keys():
   print data['EDIDep']['2809'][flights]





